<template>
  <div style="overflow: hidden">
    <Swiper :list="lubotuList"/>
    <div class="pinkunhu">
      <div class="title">
        <h1>
          Recommend<br />
          贫困户案例
        </h1>
      </div>
      <div class="pkhList">
        <div class="card" v-for="(poor, index) in PinKunHuList" :key="index" @click="skipInfo('pkh',poor.id)">
          <div class="imgBox cardItem">
            <img :src="poor.cover" />
          </div>
          <div class="contentBox cardItem">
            <span>{{ poor.name }}</span>
          </div>
        </div>
      </div>
      <div class="pkhBtn">
        <div
          class="loading"
          v-loading="loading1"
          element-loading-text="loading"
          element-loading-background="rgba(0, 0, 0, 0)"
        >
          <button @click="checkMore('poor')" v-show="!loading1">
            查看更多
          </button>
        </div>
      </div>
    </div>
    <div class="zhiyuanzhe">
      <div class="title">
        <h1>Recommend</h1>
        <h1>志愿者招聘推荐</h1>
      </div>
      <div class="zyzList">
        <div class="card" v-for="(vol, index) in ZhiYuanZeList" :key="index" @click="skipInfo('zyz',vol.id)">
          <div class="imgBox cardItem">
            <img :src="vol.picture" />
          </div>
          <div class="contentBox cardItem">
            <span>{{ vol.total }}</span>
          </div>
        </div>
      </div>
      <div class="pkhBtn">
        <div
          class="loading"
          v-loading="loading2"
          element-loading-text="loading"
          element-loading-background="rgba(0, 0, 0, 0)"
        >
          <button @click="checkMore('vol')" v-show="!loading2">查看更多</button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>

import {
  reqLunBoTuList,
  reqPinKunHuList,
  reqZhiYuanZeList,
} from "../../../api";
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 4,
      lubotuList: [],
      PinKunHuList: [],
      ZhiYuanZeList: [],
      loading1: false,
      loading2: false,
    };
  },
  created() {
    this.getShowLuBoTu();
    this.getPinKunList();
    this.gerZhiYuanZheList();
  },
  methods: {
    //获取轮播图
    async getShowLuBoTu() {
      let { pageNum ,pageSize} = this;
      let result = await reqLunBoTuList({ pageNum, pageSize});
      if (result.code != 200) return console.error("faile");
      this.lubotuList = result.data.list;
    },
    //获取贫困户列表
    async getPinKunList() {
      let { pageNum, pageSize } = this;
      let result = await reqPinKunHuList({ pageNum, pageSize });
        this.loading1 = false;
        if (result.code != 200) return console.error("faile");
        if(this.PinKunHuList.length != 0){
                if(this.PinKunHuList[this.PinKunHuList.length-1].id == result.data.list[result.data.list.length-1].id ) 
                  return this.$message({ type: "warning", message: "没有更多啦" }); 
        }
        this.PinKunHuList = [...this.PinKunHuList,...result.data.list]
    },
    //获取志愿者列表
    async gerZhiYuanZheList() {
      let { pageNum, pageSize } = this;
      let result = await reqZhiYuanZeList({ pageNum, pageSize });
      this.loading2 = false;
      if (result.code != 200) return console.error("faile");
      if(this.ZhiYuanZeList.length != 0){
          if(this.ZhiYuanZeList[this.ZhiYuanZeList.length-1].id == result.data.list[result.data.list.length-1].id ) 
            return this.$message({ type: "warning", message: "没有更多啦" }); 
      }
      this.ZhiYuanZeList = [...this.ZhiYuanZeList,...result.data.list]
    },
    //查看更多
    checkMore(type) {
      this.pageNum  += 4;
      switch (type) {
        case "poor":
          this.loading1 = true;
          this.getPinKunList();
          break;
        case "vol":
          this.loading2 = true;
          this.gerZhiYuanZheList();
          break;
      }
    },
    //跳转至详情页
    skipInfo(type,id){
      switch (type){
        case 'pkh':
          this.$router.push({name:"pkhinfo",params:{id}});
          break;
        case 'zyz':
          this.$router.push({name:"zyzinfo",params:{id}});
          break;
      }
    }
  },
};
</script>

<style lang="less" scoped>
.pinkunhu,
.zhiyuanzhe {
  width: 100%;
  margin-bottom: 100px;
  margin-top: 30px;
  .title {
    text-align: center;
    margin-bottom: 10px;
    h1 {
      color: #333;
    }
  }
  .pkhList,
  .zyzList {
    width: 70%;
    box-sizing: border-box;
    margin: 0px auto;
    .card {
      box-sizing: border-box;
      display: inline-flex;
      align-items: center;
      justify-content: space-between;
      width: 47%;
      border: 1px solid gray;
      border-radius: 5px;
      margin: 9px;
      padding: 10px;
      .cardItem {
        display: flex;
        img {
          border-radius: 50%;
          width: 120px;
          height: 120px;
        }
      }
      .contentBox {
        flex: 0.7;
        span {
          font-size: 20px;
        }
      }
    }
  }
  .pkhBtn {
    text-align: center;
    margin-top: 40px;
    font-size: 20px;
    .loading {
      height: 40px;
      button {
        border: 1px solid tomato;
        padding: 7px 18px;
        color: tomato;
        border-radius: 15px;
        outline: none;
      }
      button:hover {
        background-color: tomato;
        color: white;
      }
    }
  }
}
</style>